
#include <QStringList>

#include "Compatibilite.h"
#include "TrueLove.h"

int Compatibilite::CalculTauxCompatibilite(TrueLoveClient c1, TrueLoveClient c2)
{
	int error_success = CheckRequiredConstraints(c1, c2);
	if(error_success != 0)
		return error_success;
	Compatibilite c(c1, c2);
	return c.GetCorrespondanceCriteres() + c.GetCorrespondanceHobbys();
}

bool Compatibilite::SameCategory(QString q1, QString q2)
{
	QStringList musics, voyages, musees, sports, lectures, cinema;
	cinema << "Actions" << "Romances" << "Science fiction" << "Humour";
	lectures << "Policiers" << "Bandes dessinées" << "Fictions";
	sports << "Natation" << "Football" << "Basketball" << "Baseball" << "Roller" << "Danse";
	musics << "Rock" << "Pop" << "Valse" << "Classique";	
	voyages << "Montagne" << "Mer" << "Camping";
	musees << "Art" << "Histoire" << "Moderne" << "Sciences et vie";

	return (cinema.contains(q1) && cinema.contains(q2))	
		|| (lectures.contains(q1) && lectures.contains(q2))
		|| (sports.contains(q1) && sports.contains(q2))
		|| (musics.contains(q1) && musics.contains(q2))
		|| (voyages.contains(q1) && voyages.contains(q2))
		|| (musees.contains(q1) && musees.contains(q2));
}

int Compatibilite::CheckRequiredConstraints(TrueLoveClient c1, TrueLoveClient c2)
{
	// Vérification des contraintes requises: sexe, tranche d'âge et type de relation:	
	if(c1.GetSexe() != c2.GetSexeRecherche() || c1.GetSexeRecherche() != c2.GetSexe())
		return SEXE_MISMATCH;
	else if((c2.GetAge() - c2.GetDiffAge()) > c1.GetAge() 
		|| (c2.GetAge() + c2.GetDiffAge()) < c1.GetAge()
		|| (c1.GetAge() - c1.GetDiffAge()) > c2.GetAge()
		|| (c1.GetAge() + c1.GetDiffAge()) < c2.GetAge())
		return DIFF_AGE_MISMATCH;
	else if((c1.GetObjectifs().size() == c2.GetObjectifs().size()) == 1)
		if(c1.GetObjectifs().at(0) != c2.GetObjectifs().at(0))
			return OBJECTIF_MISMATCH;

	return 0;
}

int Compatibilite::GetDiffAge()
{
	if(GetClient1().GetAge() > GetClient2().GetAge())
		return GetClient1().GetAge() - GetClient2().GetAge();
	else
		return GetClient2().GetAge() - GetClient1().GetAge();
}


int Compatibilite::GetCorrespondanceHobbys()
{
	int taux = 0;

	// Taux des hobbies (40/100: 2 points/hobby, 1 point/hobby différent mais de même catégorie):
	for(int i=0; i<c1.GetHobbies().size(); i++)
		for(int j=0; j<c2.GetHobbies().size(); j++)
			if(c1.GetHobbies().at(i) == c2.GetHobbies().at(j))
				taux += 10;
			else if(SameCategory(c1.GetHobbies().at(i), c2.GetHobbies().at(j)))
				taux += 5;

	return taux;
}

int Compatibilite::GetCorrespondanceCriteres()
{
	// Taux des critères (60/100)
	return CalculTauxCritere(GetClient1().GetApparence(), GetClient2().GetApparence())
	 + CalculTauxCritere(GetClient1().GetCharme(), GetClient2().GetCharme())
	 + CalculTauxCritere(GetClient1().GetHumour(), GetClient2().GetHumour())
	 + CalculTauxCritere(GetClient1().GetIntelligence(), GetClient2().GetIntelligence())
	 + CalculTauxCritere(GetClient1().GetConversation(), GetClient2().GetConversation())
	 + CalculTauxCritere(GetClient1().GetGentillesse(), GetClient2().GetGentillesse());	

}

int Compatibilite::CalculTauxCritere(int v1, int v2)
{
	int min = v1, max = v2;
	if(v1 > v2)
	{
		min = v2;
		max = v1;
	}

	return (10 - (max - min));
}


